package com.team.panicgame.models.gareve;

import java.util.ArrayList;
import java.util.Collection;

import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Bounce;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.team.panicgame.models.InputStatus;

public class GareveWorld {
	private static final String TAG = GareveWorld.class.getSimpleName();

	private static final float PLAY_SOUND_INTERVAL = 1f ;
	
	private InputStatus inputStatus;
	private TweenManager tweenManager = new TweenManager();
	
	//Game Objects
	public Ship ship;
	public Collection<ShipParticle> shipParticles;
	public Collection<WallSegment> wallSegments;
	
	
	//Timers
	private float particleTime = 0f;
	private float lastTimePlayed = 0f;
	
	public float animationTime = 0f;
	
	//Sound flags
	public boolean playEngineSound;
	public boolean playExplosionSound;
	
	public GareveWorld(InputStatus inputStatus){
		this.inputStatus = inputStatus;
		ship = new Ship(20f,20f);
		shipParticles = new ArrayList<ShipParticle>();
		wallSegments = new ArrayList<WallSegment>();
		
		playEngineSound = false;
		playExplosionSound = true;
		
		loadWallSegments();
	}
	
	private void loadWallSegments(){
		wallSegments.add(new WallSegment(new Vector2(10f, 10f), new Vector2(80f, 10f)));
		wallSegments.add(new WallSegment(new Vector2(90f, 40f), new Vector2(150f, 50f)));
	}
	
	private ShipParticle createShipParticle(){
		Vector2 position = new Vector2(ship.shape.x + Ship.WIDTH / 2f, ship.shape.y + Ship.HEIGHT);
		
		float CONSTANT = Ship.HEIGHT * 0.6f;
		float RANDOM_FACTOR = Ship.HEIGHT * 0.8f;
		
		position.sub(ship.direction.tmp().mul(CONSTANT));
		
		float randomAngle = (float) (((int)(Math.random() * 123) %2 == 0?-1f:1f) * Math.random() * 30f);
		
		position.sub(ship.direction.tmp().rotate(randomAngle).mul((float) (3f + Math.random())));
				
		ShipParticle shipParticle = new ShipParticle(position.x,position.y);
		
		Tween.to(shipParticle, 1, ShipParticle.DURATION)
			.target(0f)
			.ease(Bounce.INOUT)
			.start(tweenManager);
		
		
		return shipParticle ;
	}
	
	private void manageParticles(float delta){
		particleTime += delta;
		if(particleTime >= ShipParticle.PARTICLE_PERIOD){
			particleTime -= Math.floor(particleTime / ShipParticle.PARTICLE_PERIOD) * ShipParticle.PARTICLE_PERIOD;
			shipParticles.add(createShipParticle());
		}
	}
	
	private void manageShip(float delta){
		
		if(ship.isAlive == false)return;
		
		Vector2 direction = new Vector2(0, 0);
		if(inputStatus.isLeft)direction.x = -delta;
		if(inputStatus.isRight)direction.x = +delta;
		
		if(inputStatus.isDown)direction.y = -delta;
		if(inputStatus.isUp)direction.y = +delta;
		
		if(inputStatus.isUp || inputStatus.isDown || inputStatus.isLeft || inputStatus.isRight)
			playEngineSound = true;
		
		ship.move(delta, direction);
	}
	
	private void manageCollisions(){
		if(ship.isAlive == false)
			return;
		for (WallSegment wallSegment : wallSegments){
			if(wallSegment.setCollision(ship)){
				ship.kill();
				playExplosionSound = true;
			}
		}
	}

	public void update(float delta) {
		animationTime += delta;
		
		playEngineSound = false;
		playExplosionSound = false;
		
		manageParticles(delta);
		tweenManager.update(delta);
		manageShip(delta);
		manageCollisions();		
	}
}
